package org.imlinker.status;

import javax.swing.ImageIcon;

import org.imlinker.gui.MainWindow;
import org.imlinker.login.Account;
import org.imlinker.setting.Settings;

import org.jivesoftware.smack.packet.Presence;

/**
 *  
 * 
 */
public class StatusManager {

	/**
	 * The current mode.
	 */
	private Presence.Mode mode;

	/**
	 * The current satus info.
	 */
	private String status;

	/**
	 * The refor of mainWindow.
	 */
	private MainWindow mainWindow;

	/**
	 * The sington self refer.
	 */
	private static StatusManager manager = null;

	/**
	 * The construtor.
	 * 
	 * @param window
	 *            The mainWindow.
	 */
	public StatusManager(final MainWindow window) {
		mode = Presence.Mode.available;
		status = "";
		this.mainWindow = window;
	}

	/**
	 * 
	 * @param mainWindow
	 *            MainWindow.
	 * @return The single StatusManager.
	 */
	public static StatusManager getManager(final MainWindow mainWindow) {
		if (manager == null) {
			manager = new StatusManager(mainWindow);
		}

		return manager;
	}

	/**
	 * 
	 * @param m
	 *            The presence mode need to be saved.
	 * @param info
	 *            The status information to be saved.
	 */
	public final void setStatus(final Presence.Mode m, final String info) {
		if (m != null) {
			this.mode = m;
		}

		if (info != null) {
			this.status = info;
		}

		final int presenceNice = 24;
		Account.sendPacket(new Presence(Presence.Type.available, this.status,
				presenceNice, this.mode));

		ImageIcon icon = StatusIcons.getFreeIcon();
		if (this.mode == Presence.Mode.away) {
			icon = StatusIcons.getAwayIcon();
		} else if (this.mode == Presence.Mode.dnd) {
			icon = StatusIcons.getBusyIcon();
		}

		mainWindow.setStatus(this.status, icon);
		Settings.getSettings().setValue("status", status);
	}
}
